package com.ming.sportdemo.controller;

import com.alibaba.fastjson.JSON;
import com.ming.sportdemo.bean.QueryInfo;
import com.ming.sportdemo.bean.User;
import com.ming.sportdemo.annotation.NeedFinalRole;
import com.ming.sportdemo.annotation.NeedRole;
import com.ming.sportdemo.annotation.UserToken;
import com.ming.sportdemo.common.Result;
import com.ming.sportdemo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
@CrossOrigin
@RestController
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("/allUser")
    @NeedRole
    public Result getUserList(HttpServletRequest request, QueryInfo queryInfo){
        //获取最大列表数和当前编号
        int numbers = userService.getUserCounts("%"+queryInfo.getQuery()+"%");
        int pageStart = (queryInfo.getPageNum() - 1) * queryInfo.getPageSize();
        List<User> users = userService.getAllUser("%" + queryInfo.getQuery() + "%", pageStart, queryInfo.getPageSize());
        HashMap<String, Object> res = new HashMap<>();
        res.put("numbers",numbers);
        res.put("data",users);
        String res_string = JSON.toJSONString(res);

        return Result.succ(res);
    }

    @UserToken
    @NeedFinalRole
    @RequestMapping("/updateState")
    public Result updateUserState(HttpServletRequest request,@RequestParam("id")Integer id,@RequestParam("state")Boolean state){
        int i = userService.updateState(id, state);
        String res = i>0?"success":"error";
        if(res.equals("success")){
            return Result.succ(res);
        }else {
            return Result.fail(res);
        }

    }

    @UserToken
    @NeedRole
    @RequestMapping("/addUser")
    public Result addUser(HttpServletRequest request,@RequestBody User user){
        user.setRole("普通用户");
        user.setState(false);
        int i = userService.addUser(user);
        String res = i>0?"success":"error";
        if(res.equals("success")){
            return Result.succ(res);
        }else {
            return Result.fail(res);
        }
    }

    @UserToken
    @NeedFinalRole
    @RequestMapping("/addManager")
    public Result addManager(HttpServletRequest request,@RequestBody User user){
        user.setRole("管理员");
        user.setState(false);
        int i = userService.addUser(user);
        String res = i>0?"success":"error";
        if(res.equals("success")){
            return Result.succ(res);
        }else {
            return Result.fail(res);
        }
    }

    @UserToken
    @NeedRole
    @RequestMapping("/deleteUser")
    public Result deleteUser(HttpServletRequest request,int id){
        int i = userService.deleteUser(id);
        String res = i>0?"success":"error";
        if(res.equals("success")){
            return Result.succ(res);
        }else {
            return Result.fail(res);
        }
    }

    @UserToken
    @NeedRole
    @RequestMapping("/updateUser")
    public Result updateUser(HttpServletRequest request,@RequestBody User user){
        user.setState(false);
        int i = userService.updateUser(user);
        String res = i>0?"success":"error";
        if(res.equals("success")){
            return Result.succ(res);
        }else {
            return Result.fail(res);
        }
    }
}
